package com.elitel.frame.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elitel.frame.base.service.CfgDbService;
import com.elitel.frame.base.service.CfgTableService;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgTable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/system/resourceTree")
public class ResourceTreeController {
    private static final Logger logger = LoggerFactory.getLogger(ResourceTreeController.class);
    @Autowired
    CfgTableService cfgTableService;

    @Autowired
    CfgDbService cfgDbService;

    @RequestMapping("/getJStree")
    @ResponseBody
    public JSONObject getJStree(HttpServletRequest request) {
        int mark = Integer.parseInt(request.getParameter("id")) - 1;
        JSONArray jsonArray = new JSONArray();
        List<Map<String, String>> jStreeParams = cfgTableService.getJStreeParams(mark);

        boolean hasOther = false;
        Set<String> otherDbSets = new HashSet<>();
        for (Map<String, String> map : jStreeParams) {
            Map<String, String> item = new LinkedHashMap<>();
            String id = map.get("id");
            String text = map.get("dtname");
            String parent_id = map.get("parentid");

            if (StringUtils.isBlank(parent_id)) {
                //如果cfg_table中配置的表没有目录信息，则以库名为组织归为其它类
                hasOther = true;
                String dbname = map.get("dbname");
                if (StringUtils.isBlank(dbname)) {
                    continue;
                }
                parent_id = "99_" + dbname;
                if (!otherDbSets.contains(parent_id)) {
                    Map<String, String> parentItem = new LinkedHashMap<>();
                    CfgDb cfgDb = cfgDbService.selectByKey(dbname);
                    String dbText = dbname;
                    if (cfgDb != null) {
                        String dbMark = cfgDb.getDbMark();
                        if (StringUtils.isNotBlank(dbMark)) {
                            dbText = dbMark + "(" + dbname + ")";
                        }
                    }
                    parentItem.put("id", parent_id);
                    parentItem.put("parent", "99_");
                    parentItem.put("text", dbText);

                    otherDbSets.add(parent_id);
                    jsonArray.add(parentItem);
                }
            } else {
                parent_id = parent_id.equals("00") ? "#" : parent_id;
            }
            item.put("id", id);
            item.put("parent", parent_id);
            item.put("text", text);

            if (id.length() > 30) {
                item.put("icon", "../img/icon/icon.png");
            }
            jsonArray.add(item);
        }

        if (hasOther) {
            Map<String, String> otherItem = new LinkedHashMap<>();
            otherItem.put("id", "99_");
            otherItem.put("parent", "#");
            otherItem.put("text", "其它");
            jsonArray.add(otherItem);
        }

        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", jsonArray);
        jsonObject1.put("core", jsonObject);
        return jsonObject1;
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    public CfgTable getInfo(HttpServletRequest request) {
        String id = request.getParameter("id");
        return cfgTableService.getInfoByGuid(id);
    }
}
